package com.yilin.platform.service.impl.order;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yilin.platform.base_core.application.PageData;
import com.yilin.platform.base_core.commons.utils.DateTimeUtil;
import com.yilin.platform.base_core.commons.utils.MathUtil;
import com.yilin.platform.base_core.commons.utils.ParamUtil;
import com.yilin.platform.base_core.commons.utils.StringUtil;
import com.yilin.platform.base_core.tools.QueryUtil;
import com.yilin.platform.entity.order.TransitOrdinaryOrder;
import com.yilin.platform.entity.user.Account;
import com.yilin.platform.entity.user.Organize;
import com.yilin.platform.mapper.capital.AdvanceMoneyMapper;
import com.yilin.platform.mapper.order.TransitOrdinaryOrderMapper;
import com.yilin.platform.service.impl.user.AccountService;
import com.yilin.platform.service.impl.user.OrganizeService;
import com.yilin.platform.service.order.ITransitOrdinaryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
@Transactional(rollbackFor = Exception.class)
public class TransitOrdinaryService extends ServiceImpl<TransitOrdinaryOrderMapper, TransitOrdinaryOrder> implements ITransitOrdinaryService {
    @Resource
    private TransitOrdinaryOrderMapper transitOrdinaryOrderMapper;
    @Resource
    private AdvanceMoneyMapper advanceMoneyMapper;
    @Resource
    private AccountService accountService;
    @Resource
    private OrganizeService organizeService;
    @Override
    public void saveOrUpdateTransitOrdinary(Account account, TransitOrdinaryOrder transitOrdinaryOrder) {
        account=accountService.getById(account.getId());
        Double totalCost = MathUtil.multiply(transitOrdinaryOrder.getFreightUnitPrice(), transitOrdinaryOrder.getUnloadingWeight(), 2);
        double proudCoust = MathUtil.subtract(transitOrdinaryOrder.getLoadingWeight(), transitOrdinaryOrder.getUnloadingWeight());
        transitOrdinaryOrder.setFreightCost(totalCost);
        transitOrdinaryOrder.setPoundDifference(proudCoust);
        transitOrdinaryOrder.setOrganizeId(account.getOrganizeId());
        transitOrdinaryOrder.setOrganizeName(account.getOrganizeName());
        if(StringUtil.isNotEmpty(transitOrdinaryOrder.getSuperiorPlatId())){
            Organize organize=organizeService.getOrganizeById(transitOrdinaryOrder.getSuperiorPlatId());
            ParamUtil.isNull(organize,"获取合作组织失败");
            transitOrdinaryOrder.setSuperiorPlatId(organize.getId());
            transitOrdinaryOrder.setSuperiorPlatName(organize.getOrganizeName());
        }else{
            transitOrdinaryOrder.setSuperiorPlatId(account.getSuperiorPlatId());
            transitOrdinaryOrder.setSuperiorPlatName(account.getSuperiorPlatName());
        }
        transitOrdinaryOrder.setAccountId(account.getId());
        transitOrdinaryOrder.setAccountName(account.getName());
        transitOrdinaryOrder.setTransitStatus(TransitOrdinaryOrder.TransitStatus.success);
       if(StringUtil.isEmpty(transitOrdinaryOrder.getTransitNo())) transitOrdinaryOrder.setTransitNo(DateTimeUtil.dateNo("PT"));
        transitOrdinaryOrder.setAuditingId(null);
        transitOrdinaryOrder.setAuditingName(null);
        transitOrdinaryOrder.setAuditingDate(null);
        transitOrdinaryOrder.setPaymentId(null);
        transitOrdinaryOrder.setPaymentName(null);
        transitOrdinaryOrder.setPaymentDate(null);
        transitOrdinaryOrder.setReturnRemark(null);
        saveOrUpdate(transitOrdinaryOrder);
    }

    //获取分页
    @Override
    public PageData<TransitOrdinaryOrder> getTransitOrdinaryPage(Account loginUser, TransitOrdinaryOrder query, Integer start, Integer limit) {
        QueryWrapper<TransitOrdinaryOrder> queryWrapper = new QueryWrapper<>();
        QueryUtil.buildOrganizeIdOrSuperiorPlatIdWrapper(queryWrapper,loginUser);
        QueryUtil.andQueryWrapper(queryWrapper, query);
        return transitOrdinaryOrderMapper.selectPage(new PageData<>(start, limit), queryWrapper);
    }

    //根据ID获取
    @Override
    public TransitOrdinaryOrder getTransitOrdinaryOrderById(String id) {
        return transitOrdinaryOrderMapper.selectById(id);
    }

    //审核
    @Override
    public void auditingTransitOrdinaryOrder(Account account, TransitOrdinaryOrder transitOrdinaryOrder) {
        transitOrdinaryOrder.setAuditingId(account.getId());
        transitOrdinaryOrder.setAuditingName(account.getName());
        if (transitOrdinaryOrder.getAuditingDate() == null) transitOrdinaryOrder.setAuditingDate(new Date());
        saveOrUpdate(transitOrdinaryOrder);
    }
    @Override
    public List<Map<String, Object>> getYearMonthDayMap(String[] truckId) {
        return  transitOrdinaryOrderMapper.getYearMonthDayMap(Arrays.asList(truckId),DateTimeUtil.getYear(),DateTimeUtil.getMonth(),DateTimeUtil.getDay());
    }

    @Override
    public Map<String, Object> getOrganizeOrderStatic(String organizeId) {
        return  transitOrdinaryOrderMapper.getOrganizeOrderStatic(organizeId);
    }
   //查询数据
    @Override
    public Map<String, Object> getTruckIdStatic(String truckId,String startDate,String endDate) {
        String year=DateTimeUtil.getYear();
        String month=DateTimeUtil.getMonth();
        if(StringUtil.isEmpty(startDate)) startDate=year+"-"+month+"-01";
        if(StringUtil.isEmpty(endDate))endDate=year+"-"+month+"-31";
        Map<String, Object>  orderMap= transitOrdinaryOrderMapper.getTruckIdOrderStatic(truckId,startDate,endDate);
        Map<String, Object>  capitalMap= advanceMoneyMapper.getTruckIdAdvanceStatic(truckId,startDate,endDate);
        if(capitalMap==null)capitalMap=new HashMap<>();
        if(orderMap==null)orderMap=new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        map.put("orderMap",orderMap);
        map.put("capitalMap",capitalMap);
        return  map;
    }


    //付款完成
    @Override
    public void paymentTransitOrdinaryOrder(Account account, TransitOrdinaryOrder transitOrdinaryOrder) {
        transitOrdinaryOrder.setPaymentId(account.getId());
        transitOrdinaryOrder.setPaymentName(account.getName());
        transitOrdinaryOrder.setTransitStatus(TransitOrdinaryOrder.TransitStatus.success);
        if (transitOrdinaryOrder.getPaymentDate() == null) transitOrdinaryOrder.setPaymentDate(new Date());
        saveOrUpdate(transitOrdinaryOrder);
    }

    //删除
    @Override
    public void deleteTransitOrdinaryOrder(String id) {
        transitOrdinaryOrderMapper.deleteById(id);
    }
}
